/**
 * @(#) NoticeProxy.as
 *
 * @date		2009. 03. 30.
 * @version		1.0
 * @author		Park, Juneho
 * @description	 공지사항조회 - Proxy
 * 				공지사항 목록조회 Model Logic 
 */
 package com.skt.common_hr.main.model
 {
 	import com.skt.common_hr.common.puremvc.CommonProxy;
 	import com.skt.common_hr.main.ApplicationFacade;
 	import com.skt.common_hr.main.model.vo.MainNoticeVO;
 	import com.skt.common_hr.main.model.vo.NoticeViewVO;
 	import com.wcl.utils.StringUtil;
 	
 	import mx.collections.ArrayCollection;
 	import mx.rpc.events.FaultEvent;
 	import mx.rpc.events.ResultEvent;
 	
 	
 	public class NoticeProxy extends CommonProxy
 	{
 		public static const NAME:String = "NoticeProxy";
 		
 		public var mainArr:ArrayCollection;
 		public var viewArr:ArrayCollection;
 		
 		/**
 		 * 생성자
 		 * @paran
 		 * return void
 		 * */
		public function NoticeProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 * 		- MAIN 공지사항 LIST
		 * 
		 * @param pernr 사번
		 * @return void
		 */
		public function load(rowLine:String):void
		{
			var param:Object = new Object();
			param["I_LINES"] = rowLine;
			
			loadData("Z_HR_0033", param, result, fault);
		}
		
		
		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 * 		- VIEW 공지사항 팝업 DATA LIST
		 * 
		 * @param pernr 사번
		 * @return void
		 */
		public function viewLoad(rowLine:String):void
		{
			var param:Object = new Object();
			param["I_LINES"] = rowLine;
			
			loadData("Z_HR_0033", param, viewResult, viewFault);
		}
		
		public function increaseCount(index:Number):void
		{
			var param:Object = new Object();
			param["I_BOARD_ID"]		= "01";
			param["I_CONTENT_IDX"]		= index.toString();
			trace(index);
			loadData("Z_HR_0034", param, countResult, countFault); 
		}

		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event Listeners
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 		- MAIN 공지사항 LIST
		 * @param
		 * @return void
		 */
		private function result(event:ResultEvent):void
		{
			if(retCode == 0) {
				mainArr = parseNoticeList(event.result.TBL_BOARD);
				facade.sendNotification(ApplicationFacade.LOAD_NOTICE_MAINLIST_COMPLETE, this);
			} else {
				facade.sendNotification(ApplicationFacade.LOAD_NOTICE_MAINLIST_FAILED, this);
			}
		}
		
		private function fault(event:FaultEvent):void
		{
			facade.sendNotification(ApplicationFacade.LOAD_NOTICE_MAINLIST_FAILED, this);
		}
		
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 		- VIEW 공지사항 LIST
		 * @param
		 * @return void
		 */
		private function viewResult(event:ResultEvent):void
		{
			if(retCode == 0) {
				viewArr = parseViewList(event.result.TBL_BOARD);
				facade.sendNotification(ApplicationFacade.LOAD_NOTICE_VIEWLIST_COMPLETE, this);
			} else {
				facade.sendNotification(ApplicationFacade.LOAD_NOTICE_VIEWLIST_FAILED, this);
			}
		}
		
		private function viewFault(event:FaultEvent):void
		{
			facade.sendNotification(ApplicationFacade.LOAD_NOTICE_VIEWLIST_FAILED, this);
		}
		
		public function countResult(event:ResultEvent):void
		{
			if(retCode==0) {
				facade.sendNotification(ApplicationFacade.UPDATE_COUNT_COMPLETE, this);
			}
			else {
				facade.sendNotification(ApplicationFacade.UPDATE_COUNT_FAILED, this);
			}
		}
		
		
		private function countFault(event:FaultEvent):void
		{
			facade.sendNotification(ApplicationFacade.UPDATE_COUNT_FAILED, this);
		}
		
		
		
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Data Parsing functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		
		/**
		 * 서버에서 전달된 result 객체를 분석하여 Value Object를 생성
		 * 
		 * @param
		 * @return 공지사항 LIST Value Object
		 */
		 private function parseNoticeList(noticeData:ArrayCollection):ArrayCollection
		 {
		 	var noticeArrList:ArrayCollection = new ArrayCollection();
		 	var leng:int = noticeData.length;
		 	var vo:MainNoticeVO;
		 	
		 	for(var i:int = 0; i < 4; i++) {
			 	
			 	vo = new MainNoticeVO();
			 	if(leng > i) {
					vo.BOARD_ID		= noticeData[i].BOARD_ID;	//게시판 유형(번호범위 일련번호)
					vo.TITLE		= noticeData[i].TITLE;		//제목
					vo.FILE_NAME	= noticeData[i].FILE_NAME;	//파일이름
					vo.CONTENT_IDX	= Number(noticeData[i].CONTENT_IDX).toString();			//게시물 일련번호
					vo.CREATE_DATE	= noticeData[i].CREATE_DATE;	//게시일자(수정일자)
					vo.CREATE_DATE	= vo.CREATE_DATE.substring(0, 10);
					noticeArrList.addItem(vo);
			 	}
		 	}
		 	return noticeArrList;
		}
		
		private function parseViewList(arrList:ArrayCollection):ArrayCollection
		{
			var dataList:ArrayCollection = new  ArrayCollection();
			
			for each(var itemObj:Object in arrList) {
				
				var vo:NoticeViewVO = new NoticeViewVO();
				
				vo.CONTENT_IDX		= itemObj.CONTENT_IDX;							//게시물 일련번호
				vo.TITLE			= itemObj.TITLE;								//게시물 제목
				vo.WRITER_IDNUM		= itemObj.WRITER_IDNUM;							//사원 번호
				vo.FILE_NAME		= StringUtil.trim(itemObj.FILE_NAME);							//확장명 포함된 파일이름
				vo.CONTENT			= itemObj.CONTENT;								//내용
				vo.WRITER_NAME		= itemObj.WRITER_NAME;							//사원 또는 지원자의 포맷된 이름
				vo.BEGDA			= itemObj.BEGDA;		//시작일
				vo.BEGDA			= vo.BEGDA.substring(0, 10);
				
				vo.ENDDA			= itemObj.ENDDA;		//종료일
				vo.ENDDA			= vo.ENDDA.substring(0, 10);
				
				vo.CREATE_DATE		= itemObj.CREATE_DATE;//게시일자(수정일자)
				vo.CREATE_DATE		= vo.CREATE_DATE.substring(0, 10);
				vo.FILE_URL			= StringUtil.trim(itemObj.FILE_URL);								//다운로드 경로
				
				dataList.addItem(vo);
			}
			return dataList;
		}
 	}
 }